/**
 * Project Name:fastPlat
 * File Name:SysLogInterceptor.java
 * Package Name:com.fast.intercept
 * Date:2015年3月5日下午10:51:24
 * Copyright (c) 2015, lsflu@126.com All Rights Reserved.
 *
*/

package com.fast.user.web.interceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fast.base.util.LogUtils;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.fast.cms.service.system.SysLogService;

/**
 * ClassName:SysLogInterceptor <br/>
 * 用途: 登录拦截器 <br/>
 * 开发人员：lushuifa  http://lushuifa.iteye.com/
 * 邮箱:lsflu@126.com
 * @version  
 * @since    JDK 1.6
 * @see
 * Date:     2015年3月5日 下午10:51:24 <br/> 	 
 */
public class LoginInterceptor implements HandlerInterceptor {
	
	
	
	@Resource(name="sysLogService")
	protected SysLogService logS;

	/** 
	 * 第一执行
     * 在业务处理器处理请求之前被调用  
     * 如果返回false  
     *     从当前的拦截器往回执行所有拦截器的afterCompletion(),再退出拦截器链  
     * 如果返回true  
     *    执行下一个拦截器,直到所有的拦截器都执行完毕  
     *    再执行被拦截的Controller  
     *    然后进入拦截器链,  
     *    从最后一个拦截器往回执行所有的postHandle()  
     *    接着再从最后一个拦截器往回执行所有的afterCompletion()  
     */  
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			Object object) throws Exception {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		try{
			String uri = request.getRequestURI();
			LogUtils.info("LoginInterceptor.uri:"+uri);
			HttpSession session = request.getSession();
			if (session.getAttribute("adminUser") != null
					||uri.contains("adminLogin")
					||uri.contains("login.do")
					||uri.contains("/app/")
					||uri.contains(".out")) {
				return true;
			}else {
				response.setContentType("text/html;charset=UTF-8");
				String jsHtml =  "<script type=\"text/javascript\">"+
							         "alert(\"系统监测到你未登录,或登录信息丢失,请先登录再操作！\");"+
							         "top.location.href=\""+request.getContextPath()+"/adminLogin/login.html\";"+
							     "</script>";
				response.getWriter().print(jsHtml);
				return false;
			}
		}catch(Exception e){
			LogUtils.error("拦截器有异常",e);
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 第二执行
	 * 在业务处理器处理请求执行完成后,生成视图之前执行的动作  
	 * TODO 简单描述该方法的实现功能（可选）.
	 * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response,
			Object object, ModelAndView modelAndView) throws Exception {
		
	}
	
	/**
	 * 第三执行  
     * 在DispatcherServlet完全处理完请求后被调用   
     * 当有拦截器抛出异常时,会从当前拦截器往回执行所有的拦截器的afterCompletion()  
     */ 
	@Override
	public void afterCompletion(HttpServletRequest request,HttpServletResponse response, Object object, Exception e)
			throws Exception {
		
	}
}

